package net.cruciblesoftware.ghostspacekillah;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.lang.reflect.Field;
import java.util.Random;
import java.util.ArrayList;

class Assets {
    private static Bitmap[] playerShips;
    private static Bitmap[] enemyShips;
    private static Bitmap[] explosions;
    private static Bitmap[] bullets;
    private static Bitmap[] backgrounds;

    private static Random rand;
    private static boolean loaded = false;

    public static void load(Resources res) {
        // sanity check
        if(loaded)
            return;
        logi("loading images...");

        // do a little reflection -- just thinking about life
        Class<R.drawable> c = R.drawable.class;
        Field[] fields = c.getDeclaredFields();

        // make tmp ArrayLists for loading drawables
        ArrayList<Bitmap> ps = new ArrayList<Bitmap>(fields.length);
        ArrayList<Bitmap> es = new ArrayList<Bitmap>(fields.length);
        ArrayList<Bitmap> exp = new ArrayList<Bitmap>(fields.length);
        ArrayList<Bitmap> blt = new ArrayList<Bitmap>(fields.length);
        ArrayList<Bitmap> bg = new ArrayList<Bitmap>(fields.length);

        // load the drawables into the ArrayLists
        try {
            for(Field f : fields) {
                // decode the name
                String s = f.getName();
                boolean isShip = s.contains("ship");
                boolean isPlayer = s.contains("player");
                boolean isExplosion = s.contains("explosion");
                boolean isBullet = s.contains("bullet");
                boolean isBackground = s.contains("background");

                // load image to right array
                if(isShip && isPlayer)
                    ps.add(BitmapFactory.decodeResource(res, f.getInt(null)));
                if(isShip && !isPlayer)
                    es.add(BitmapFactory.decodeResource(res, f.getInt(null)));
                if(isExplosion)
                    exp.add(BitmapFactory.decodeResource(res, f.getInt(null)));
                if(isBullet)
                    blt.add(BitmapFactory.decodeResource(res, f.getInt(null)));
                if(isBackground)
                    bg.add(BitmapFactory.decodeResource(res, f.getInt(null)));
                if(isShip || isExplosion || isBullet || isBackground)
                    logi("\tloaded image: " + s);
            }
        } catch(IllegalAccessException e) {
            logw("could not access resource: " + e.toString());
        }

        // set static members
        playerShips = (Bitmap[])ps.toArray(new Bitmap[ps.size()]);
        enemyShips = (Bitmap[])es.toArray(new Bitmap[es.size()]);
        explosions = (Bitmap[])exp.toArray(new Bitmap[exp.size()]);
        bullets = (Bitmap[])blt.toArray(new Bitmap[blt.size()]);
        backgrounds = (Bitmap[])bg.toArray(new Bitmap[bg.size()]);
        rand = new Random();
        loaded = true;
    }


    // needed?
    public static void unload() {
        playerShips = null;
        enemyShips = null;
        explosions = null;
        bullets = null;
        backgrounds = null;
    }


    public static Bitmap getBitmap(int type) {
        if(!loaded) {
            loge("calling getBitmap() before loading images!");
            return null;
        }
        switch(type) {
        case FieldActor.TYPE_PLAYER:
            return playerShips[rand.nextInt(playerShips.length)];
        case FieldActor.TYPE_ENEMY:
            return enemyShips[rand.nextInt(enemyShips.length)];
        case FieldActor.TYPE_BULLET:
            return bullets[rand.nextInt(bullets.length)];
        case FieldActor.TYPE_EXPLOSION:
            return explosions[rand.nextInt(explosions.length)];
        case SceneBackground.TYPE_BACKGROUND:
            return backgrounds[rand.nextInt(backgrounds.length)];
        default:
            logw("tried getting ship image of non-existing type " + type);
        }
        return null;
    }


    /* Logging functions */
    private static void logi(String msg) {
        android.util.Log.i(Assets.class.getName(), msg);
    }
    private static void logw(String msg) {
        android.util.Log.w(Assets.class.getName(), msg);
    }
    private static void loge(String msg) {
        android.util.Log.e(Assets.class.getName(), msg);
    }
}
